unit UnFrmCotacoes;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, DBGrids, ExtCtrls, DB, DBClient;

type
  TFrmCotacoes = class(TForm)
    CdsAcoes: TClientDataSet;
    CdsAcoesacao: TStringField;
    CdsAcoespreco: TCurrencyField;
    CdsAcoessinal: TStringField;
    DsAcoes: TDataSource;
    TmCotacao: TTimer;
    DBGrid1: TDBGrid;
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TmCotacaoTimer(Sender: TObject);
    procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure DBGrid1DblClick(Sender: TObject);
  private
    procedure FormatarArquivo2(Arq: string);
  public

  end;


implementation

uses UntDm, UntFrmGrafico;

{$R *.dfm}

procedure TFrmCotacoes.DBGrid1DblClick(Sender: TObject);
var g: TFrmGraf;
begin
  g := TFrmGraf.Create(Self);

  g.acao := CdsAcoesacao.AsString;
  g.Caption := g.acao;
  g.Show;
end;

procedure TFrmCotacoes.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var holdColor: TColor;
begin
  holdColor := DBGrid1.Canvas.Brush.Color;
  if Column.FieldName = 'preco' then begin
    if CdsAcoessinal.AsString = '+' then DBGrid1.Canvas.Brush.Color := clGreen
    else if CdsAcoessinal.AsString = '-' then DBGrid1.Canvas.Brush.Color := clRed;
    DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
    DBGrid1.Canvas.Brush.Color := holdColor;
  end;
end;

procedure TFrmCotacoes.FormCreate(Sender: TObject);
begin
  CdsAcoes.CreateDataSet;
end;

procedure TFrmCotacoes.FormDestroy(Sender: TObject);
begin
  CdsAcoes.EmptyDataSet;
end;

procedure TFrmCotacoes.TmCotacaoTimer(Sender: TObject);
var arq: string;
begin
  try
    TmCotacao.Enabled := False;
    TmCotacao.Interval := 10000;
    arq := DM.path + '\3.csv';
    DeleteFile(arq);
    DM.download('http://www.bmfbovespa.com.br/Pregao-OnLine/ExecutaAcaoCarregarDados.asp?CodDado=Ticker', arq);
    if FileExists(arq) then FormatarArquivo2(Arq);
  finally
    TmCotacao.Enabled := True;
    DeleteFile(arq);
  end;
end;

procedure TFrmCotacoes.FormatarArquivo2(Arq: string);
var f: TextFile;
    s,rec: string;
    a,b,i: integer;
    l1,l2: TStrings;
begin
  try
    l1 := TStringList.Create;
    l2 := TStringList.Create;
    AssignFile(F,Arq);
    Reset(F);
    Readln(F,s);
    a := pos('v=',s)+2;
    b := pos('&d=',s);
    s := Copy(s,a,b-a);

    l1.Delimiter := '|';
    l2.Delimiter := '@';
    l1.DelimitedText := s;
    rec := CdsAcoesacao.AsString;
    CdsAcoes.EmptyDataSet;
    for i := 0 to (l1.Count-1) do begin
      l2.DelimitedText := l1[i];
      with CdsAcoes do begin
        Append;
        CdsAcoesacao.AsString := l2[0];
        if Pos('-',l2[1]) > 0 then CdsAcoessinal.AsString := '-'
        else if Pos('#',l2[1]) > 0 then CdsAcoessinal.AsString := '+';
        l2[1] := StringReplace(l2[1],'.',',',[]);
        l2[1] := StringReplace(l2[1],'#','',[]);
        l2[1] := StringReplace(l2[1],'-','',[]);
        l2[1] := StringReplace(l2[1],'*','',[]);
        CdsAcoespreco.AsCurrency := StrToCurr(l2[1]);
        Post;
      end;
    end;
    CdsAcoes.Locate('acao',rec,[]);
  finally
    l1.Free;
    l2.Free;
    CloseFile(F);
  end;
end;

end.
